//一个ipad对象
function Ipad(lid, title) {
    // Product = function(lid,title){
    if (lid < 0) {
        return {};
    } else if (title == undefined) {
        this.title = "未命名的标题项";
    }
    this.lid = lid
    //商品标题 
    this.title = title;
    //商品价格（最低价/基础价）默认0
    this.price = 0;
    //商品在商品列表的展示图片 正方形 默认是空
    this.shoplistImg = "";
    //副标题 类似于型号 内存之类的 是一个数组，用来存放全部副标题
    this.exTitle = ["", "", "", ""]
    //所有价格，增量与减量共存 数组长度与商品属性息息相关
    this.exPrice = [0, 0, 0, 0]

    //商品的4大属性，默认都是0 如果不给传参就是默认
    //型号对象
    this.info = {
        title: "",
        price: 0
    };
    //外观对象
    this.appear = {
        title: "",
        price: 0
    };
    //存储大小对象
    this.memory = {
        title: "",
        price: 0
    };
    //连接类型对象
    this.connect = {
        title: "",
        price: 0
    }

    //获取各种配置的方法 支持链式语法
    //this.product.setInfo({x:1,y:2}).setAppear("棕色").setMemory(256)
    this.setInfo = function (i) {
        this.isLegal(i)
        this.info = i
        this.exTitle[0] = i.title;
        this.exPrice[0] = i.price;
        return this;
    }

    this.setAppear = function (a) {
        this.isLegal(a)
        this.appear = a;
        this.exPrice[1] = a.price;
        this.exTitle[1] = a.title;
        return this;
    }

    this.setMemory = function (m) {
        this.isLegal(m)
        this.memory = m;
        this.exTitle[2] = m.title;
        this.exPrice[2] = m.price;
        return this;
    }

    this.setConnect = function (c) {
        this.isLegal(c)
        this.connect = c;
        this.exTitle[3] = c.title;
        this.exPrice[3] = c.price;
        return this;
    }

    //判断给进来的配置对象是否符合规定
    //title 和 price 都要有
    this.isLegal = function (i) {
        if (i.title == undefined) {
            throw new Error("product 没有 title 标题");
        } else if (i.price == undefined) {
            //如果没有price参数，则直接复制默认0
            //给一个警告
            i.price = 0;
            console.warn(`没有price参数 在 ${i.title} 上`);
            //   throw new Error("product 没有 price 价格");
        } else if (i == {}) {
            throw new Error("product 是空对象");
        }
        return true;
    }

    //将主标题跟副标题拼接取来 这个基本上用在购物车列表里，显示商品细节的时候
    //类似于  ipadPro 11英寸 256g 蜂窝机型 
    this.getAllTitle = function () {
        let all = "";
        this.exTitle.forEach(e => {
            if (e == "" || e == undefined) {
                return
            } else {
                all += ` ${e}`;
            }
        });
        // return this.title + all;
        return all;
    }

    this.getPrice = function () {
        // let sum;
        this.price = this.exPrice.reduce((prev, cur) => {
            return prev + cur;
        }, 0)
        return this.price;
    }
    this.install = function () {
        console.log("使用了pro");
    }
}

//一个mac对象
function Mac(lid, title) {
    // Product = function(lid,title){
    if (lid < 0) {
        return {};
    } else if (title == undefined) {
        this.title = "未命名的标题项";
    }
    this.lid = lid
    //商品标题 
    this.title = title;
    //商品价格（最低价/基础价）默认0
    this.price = 0;
    //商品在商品列表的展示图片 正方形 默认是空
    this.shoplistImg = "";
    //副标题 类似于型号 内存之类的 是一个数组，用来存放全部副标题
    this.exTitle = ["", "", "","","","","",""]
    //所有价格，增量与减量共存 数组长度与商品属性息息相关
    this.exPrice = [0, 0, 0, 0, 0,0,0,0,0,0]

    //mac商品的5大属性，默认都是0 如果不给传参就是默认
    //内存对象 8g/16g
    this.ram = {
        title: "",
        price: 0
    };
    //键盘语言对象
    this.keybord = {
        title: "",
        price: 0
    };
    //存储大小对象
    this.memory = {
        title: "",
        price: 0
    };
    //预装软件Final Cut Pro
    //预装软件是不会在title里显示的 
    this.cut_Pro = {
        title: "",
        price: 0
    }
    //预装软件 Logic Pro
    this.logic_Pro = {
        title: "",
        price: 0
    }
    //以太网 imac
    this.internet = {
        title:"",
        price:0
    }
    //蓝色鼠标或触控板 imac
    this.bluemouse = {
        title:"",
        price:0
    }
    //蓝色键盘 imac
    this.blueKeybord = {
        title:"",
        price:0
    }
    //SoC 芯片 (处理器)
    this.soc = {
        title:"",
        price:0
    }
    //电源适配器
    this.power = {
        title:"",
        price:0
    }

    //获取各种配置的方法 支持链式语法
    //this.product.setInfo({x:1,y:2}).setAppear("棕色").setMemory(256)
    this.setArm = function (a) {
        this.isLegal(a)
        this.ram = a
        this.exTitle[0] = a.title;
        this.exPrice[0] = a.price;
        return this;
    }

    this.setMemory = function (m) {
        this.isLegal(m)
        this.memory = m;
        this.exTitle[1] = m.title;
        this.exPrice[1] = m.price;
        return this;
    }
    //键盘语言是不会在title里显示的
    this.setKeybord = function (k) {
        this.isLegal(k)
        this.keybord = k;
        this.exPrice[2] = k.price;
        // this.exTitle[2] = k.title;
        return this;
    }

    //预装软件是不会在title里显示的 
    this.setCutPro = function (c) {
        this.isLegal(c)
        this.cut_Pro = c;
        //   this.exTitle[3] = c.title;
        this.exPrice[3] = c.price;
        return this;
    }

    //预装软件是不会在title里显示的 
    this.setLogicPro = function (l) {
        this.isLegal(l)
        this.logic_Pro = l;
        //   this.exTitle[3] = c.title;
        this.exPrice[4] = l.price;
        return this;
    }

    this.setInternet = function(i){
        this.isLegal(i)
        this.internet = i;
        this.exTitle[3] = i.title;
        this.exPrice[5] = i.price;
        return this;
    }

    this.setBluemouse = function(i){
        this.isLegal(i)
        this.bluemouse = i;
        this.exTitle[4] = i.title;
        this.exPrice[6] = i.price;
        return this;
    }

    this.setBlueKeybord = function(i){
        this.isLegal(i)
        this.blueKeybord = i;
        this.exTitle[5] = i.title;
        this.exPrice[7] = i.price;
        return this;
    }

    this.setSoc = function(i){
        this.isLegal(i)
        this.soc = i;
        this.exTitle[6] = i.title;
        this.exPrice[8] = i.price;
        return this;
    }

    this.setPower = function(i){
        this.isLegal(i)
        this.power = i;
        this.exTitle[7] = i.title;
        this.exPrice[9] = i.price;
        return this;
    }

    //判断给进来的配置对象是否符合规定
    //title 和 price 都要有
    this.isLegal = function (i) {
        if (i.title == undefined) {
            throw new Error("product 没有 title 标题");
        } else if (i.price == undefined) {
            //如果没有price参数，则直接复制默认0
            //给一个警告
            i.price = 0;
            console.warn(`没有price参数 在 ${i.title} 上`);
            //   throw new Error("product 没有 price 价格");
        } else if (i == {}) {
            throw new Error("product 是空对象");
        }
        return true;
    }

    //将主标题跟副标题拼接取来 这个基本上用在购物车列表里，显示商品细节的时候
    //类似于  ipadPro 11英寸 256g 蜂窝机型 
    this.getAllTitle = function () {
        let all = "";
        this.exTitle.forEach(e => {
            if (e == "" || e == undefined) {
                return
            } else {
                all += ` ${e}`;
            }
        });
        return this.title + all;
    }

    //获取到相加后的全部总价
    this.getPrice = function () {
        // let sum;
        this.price = this.exPrice.reduce((prev, cur) => {
            return prev + cur;
        }, 0)
        return this.price;
    }
    this.install = function () {
        console.log("使用了pro");
    }
}
export default {
    Ipad, Mac
};